/*$(function(){
	//utype,umoney,uhouse,ucar,uaf;
	console.log(getQueryVariable("price"));
	console.log(getQueryVariable("utype"));
	console.log(decodeURI(getQueryVariable("umoney")));
	console.log(getQueryVariable("uhouse"));
	console.log(getQueryVariable("ucar"));
	console.log(getQueryVariable("uaf"));
})*/
//获取当前页url参数
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if(pair[0] == variable) {
			return pair[1];
		}
	}
	return(false);
}

var checkPhone = function(a) {
	var patrn = /^((?:13|15|18|14|17)\d{9}|0(?:10|2\d|[3-9]\d{2})[1-9]\d{6,7})$/;
	if(!patrn.exec(a)) return false;
	return true;
};

/*提示信息*/
function myalert(tip) {
	$('.tipcont').css('display', 'block');
	setTimeout(function() {
		$('.tipcont').css('display', 'none');
	}, 2000);
	$('.tipcont').text(tip)

}
/*遮罩显示*/
function tipshow() {
	$('.zhezhao').css('display', 'block');
}
//验证
var loanMoney, loanName, inputGroupSelect01, loanTelphone, loanShortYan;
function submitverify() {
	//下一步之前的所有验证
	loanMoney = $('#loanMoney').val();
	loanName = $('#loanName').val();
	inputGroupSelect01 = $('#inputGroupSelect01').find("option:selected").text();
	loanTelphone = $('#loanTelphone').val();
	loanShortYan = $('#loanShortYan').val();
	if(loanMoney != '') {
		if(loanName != '' && loanName.length < 5) {
			if(inputGroupSelect01 == '男' || inputGroupSelect01 == '女') {
				if(checkPhone(loanTelphone)) {
					if(loanShortYan==""||loanShortYan==null) {
						myalert('请输入验证码');
						return false;
					} else {
						return true;
					}
				} else {
					myalert('请输入正确的手机号');
					return false;
				}
			} else {
				myalert('请选择性别');
				return false;
			}
		} else {
			myalert('请输入正确的姓名');
			return false;
		}
	} else {
		myalert('请输入您所需的金额');
		return false;
	}
}
$('.submit').click(function() {
	if(submitverify()) {
		if (inputGroupSelect01=="女") {
			inputGroupSelect01=0;
		}else{
			inputGroupSelect01=1;
		}
		var jsondata = {"ranNum":19502,
						"forecastType":getQueryVariable("utype"),
						"forecastMoney":getQueryVariable("umoney"),
						"forecastHouse":getQueryVariable("uhouse"),
						"forecastCar":getQueryVariable("ucar"),
						"forecastAf":getQueryVariable("uaf"),
						"forecastNeedMoney":loanMoney,
						"forecastName":loanName,
						"forecastSex":inputGroupSelect01,
						"price":getQueryVariable("price")}
		$.post("/adduserinfo.action",jsondata,function(data){
			if (data.code==0) {
				sessionStorage.setItem("prolist",JSON.stringify(data));
				window.location.href="project.html";
			}else{
				myalert(data.msg);
			}
		})
	}
})
var count = 59;
var countdown;
//时间递减
function down() {
	$('.huo').html(count + "s");
	if(count == 0) {
		$(".huo").text("重新获取").removeAttr("disabled");
		clearInterval(countdown);
	}
	count--;
}

window.callback = function(res) {
	// res（用户主动关闭验证码）= {ret: 2, ticket: null}
	// res（验证成功） = {ret: 0, ticket: "String", randstr: "String"}
	if(res.ret === 0) {
		//验证是否输入正确
		if (submitverify()) {
			$.post("/sendmsg.action",{"userPhone":loanTelphone,"ticket":res.ticket,"randstr":res.randstr},function(data){
				console.log(data);
				if (data.code) {
					countdown = setTimeout(down(),1000);
				}else{
					myalert(data.msg);
				}
			})
		}
	}
}


